/**
 * Project Name:OrderHandler.java
 * File Name:OrderHandler.java
 * Date:2019/3/29 17:13
 * Copyright (c) 2019, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball.order.handler;

import com.alibaba.fastjson.JSON;
import com.meatball.base.repository.matcher.MeatballExample;
import com.meatball.base.repository.matcher.MeatballMatcher;
import com.meatball.constant.MeatballConst;
import com.meatball.order.entity.SerialOrder;
import com.meatball.order.repository.SerialOrderRepository;
import com.meatball.utils.DateUtil;
import com.meatball.utils.response.ResultUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @Title: OrderHandler.java
 * @Description: todo(订单业务层)
 * @Author: 張翔宇
 * @Date: 2019/3/29 17:13
 * @Version: V1.0
 */
@Log4j2
@Component
public class OrderHandler {
    private final SerialOrderRepository serialOrderRepository;

    public OrderHandler (SerialOrderRepository serialOrderRepository) {
        this.serialOrderRepository = serialOrderRepository;
    }

    /**
     * @title: seriaList
     * @description: todo(无分页)
     * @params: [request]
     * @return: reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>    返回类型
     * @author: 張翔宇
     * @date: 2019/4/13 17:25
     */
    public Mono<ServerResponse> seriaList (ServerRequest request) {
        Date startTime = DateUtil.minimumParse(request.queryParam("startTime").get());
        Date endTime = DateUtil.maximumParse(request.queryParam("endTime").get());
        String params = JSON.toJSONString(request.queryParams().toSingleValueMap());
        log.info("请求参数：{}", params);
        SerialOrder serialOrder = JSON.parseObject(params, SerialOrder.class);
        MeatballMatcher meatballMatcher = MeatballMatcher.matching()
                .withMatcher("creator", matcher -> matcher.contains())
                .withMatcher("createdTime", matcher -> matcher.between(startTime, endTime));
        return ResultUtils.response(MeatballConst.ResultCode.SUCCESS.CODE,
                MeatballConst.ResultCode.SUCCESS.MESSAGE,
                serialOrderRepository.findList(MeatballExample.of(serialOrder, meatballMatcher),
                        Sort.by(Sort.Direction.DESC, "createdTime")));
    }

    /**
     * @title: serialList
     * @description: todo(查询流水集合)
     * @params: [request]
     * @return: reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>    返回类型
     * @author: 張翔宇
     * @date: 2019/3/29 17:14
     */
    public Mono<ServerResponse> serialPage (ServerRequest request) {
        int page = Integer.parseInt(request.queryParam("page").get());
        int size = Integer.parseInt(request.queryParam("size").get());
        Date startTime = DateUtil.minimumParse(request.queryParam("startTime").get());
        Date endTime = DateUtil.maximumParse(request.queryParam("endTime").get());
        String params = JSON.toJSONString(request.queryParams().toSingleValueMap());
        log.info("请求参数：{}", params);
        SerialOrder serialOrder = JSON.parseObject(params, SerialOrder.class);
        int[] a = {0,2,3,4};
        MeatballMatcher meatballMatcher = MeatballMatcher.matching()
                .withMatcher("creator", matcher -> matcher.contains())
                .withMatcher("paymentType", matcher -> matcher.in(a))
                .withMatcher("createdTime", matcher -> matcher.between(startTime, endTime));
//                .withMatcher("lastModifiedTime", matcher -> matcher.between(endTime, endTime));

        return ResultUtils.response(serialOrderRepository.findPage(MeatballExample.of(serialOrder, meatballMatcher),
                PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdTime"))));
    }
}
